home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / editor / EdHLineProps.js < prev    next >
Encoding:
JavaScript  |  2001-06-08  |  5.9 KB  |  204 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  */
  22.  
  23. var tagName = "hr";
  24. var hLineElement;
  25. var width;
  26. var height;
  27. var align;
  28. var shading;
  29. var dialog;
  30.  
  31. // dialog initialization code
  32. function Startup()
  33. {
  34.   if (!InitEditorShell())
  35.     return;
  36.  
  37.   doSetOKCancel(onOK, onCancel);
  38.  
  39.   // Get the selected horizontal line
  40.   hLineElement = editorShell.GetSelectedElement(tagName);
  41.  
  42.   if (!hLineElement) {
  43.     // We should never be here if not editing an existing HLine
  44.     window.close();
  45.     return;
  46.   }
  47.   // Create dialog object to store controls for easy access
  48.   dialog = new Object;
  49.   dialog.heightInput = document.getElementById("height");
  50.   dialog.widthInput = document.getElementById("width");
  51.   dialog.leftAlign = document.getElementById("leftAlign");
  52.   dialog.centerAlign = document.getElementById("centerAlign");
  53.   dialog.rightAlign = document.getElementById("rightAlign");
  54.   dialog.shading = document.getElementById("3dShading");
  55.   dialog.pixelOrPercentMenulist = document.getElementById("pixelOrPercentMenulist");
  56.  
  57.   // Make a copy to use for AdvancedEdit and onSaveDefault
  58.   globalElement = hLineElement.cloneNode(false);
  59.  
  60.   // Initialize control values based on existing attributes
  61.   InitDialog()
  62.  
  63.   // SET FOCUS TO FIRST CONTROL
  64.   SetTextboxFocus(dialog.widthInput);
  65.  
  66.   // Resize window
  67.   window.sizeToContent();
  68.  
  69.   SetWindowLocation();
  70. }
  71.  
  72. // Set dialog widgets with attribute data
  73. // We get them from globalElement copy so this can be used
  74. //   by AdvancedEdit(), which is shared by all property dialogs
  75. function InitDialog()
  76. {
  77.   // Just to be confusing, "size" is used instead of height
  78.   var height = globalElement.getAttribute("size");
  79.   if(!height) {
  80.     height = 2; //Default value
  81.   }
  82.  
  83.   // We will use "height" here and in UI
  84.   dialog.heightInput.value = height;
  85.  
  86.   // Get the width attribute of the element, stripping out "%"
  87.   // This sets contents of menulist (adds pixel and percent menuitems elements)
  88.   dialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, hLineElement, "width","pixelOrPercentMenulist");
  89.  
  90.   align = globalElement.getAttribute("align").toLowerCase();
  91.  
  92.   dialog.centerAlign.checked = (align == "center" || !align);
  93.   dialog.rightAlign.checked  = (align == "right");
  94.   dialog.leftAlign.checked   = (align == "left");
  95.  
  96.   // This is tricky! Since the "noshade" attribute doesn't have a value,
  97.   //  we can't use getAttribute to figure out if it's set!
  98.   // This gets the attribute NODE from the attributes NamedNodeMap
  99.   if (globalElement.attributes.getNamedItem("noshade"))
  100.     dialog.shading.checked = false;
  101.   else
  102.     dialog.shading.checked = true;
  103.  
  104. }
  105.  
  106. function onSaveDefault()
  107. {
  108.   // "false" means set attributes on the globalElement,
  109.   //   not the real element being edited
  110.   if (ValidateData()) {
  111.     var prefs = GetPrefs();
  112.     if (prefs) {
  113.  
  114.       var alignInt;
  115.       if (align == "left") {
  116.         alignInt = 0;
  117.       } else if (align == "right") {
  118.         alignInt = 2;
  119.       } else {
  120.         alignInt = 1;
  121.       }
  122.       prefs.SetIntPref("editor.hrule.align", alignInt);
  123.  
  124.       var percentIndex = width.search(/%/);
  125.       var percent;
  126.       var widthInt;
  127.       if (width)
  128.       {
  129.         if (percentIndex > 0) {
  130.           percent = true;
  131.           widthInt = Number(width.substr(0, percentIndex));
  132.         } else {
  133.           percent = false;
  134.           widthInt = Number(width);
  135.         }
  136.       }
  137.       else
  138.       {
  139.         percent = true;
  140.         widthInt = Number(100);
  141.       }
  142.       prefs.SetIntPref("editor.hrule.width", widthInt);
  143.       prefs.SetBoolPref("editor.hrule.width_percent", percent);
  144.  
  145.       // Convert string to number
  146.       prefs.SetIntPref("editor.hrule.height", Number(height));
  147.  
  148.       prefs.SetBoolPref("editor.hrule.shading", shading);
  149.  
  150.       // Write the prefs out NOW!
  151.       prefs.savePrefFile(null);
  152.     }
  153.     }
  154. }
  155.  
  156. // Get and validate data from widgets.
  157. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  158. function ValidateData()
  159. {
  160.   // Height is always pixels
  161.   height = ValidateNumber(dialog.heightInput, null, 1, maxPixels,
  162.                           globalElement, "size", false);
  163.   if (gValidationError)
  164.     return false;
  165.  
  166.   width = ValidateNumber(dialog.widthInput, dialog.pixelOrPercentMenulist, 1, maxPixels, 
  167.                          globalElement, "width", false);
  168.   if (gValidationError)
  169.     return false;
  170.  
  171.   align = "left";
  172.   if (dialog.centerAlign.checked) {
  173.     // Don't write out default attribute
  174.     align = "";
  175.   } else if (dialog.rightAlign.checked) {
  176.     align = "right";
  177.   }
  178.   if (align)
  179.     globalElement.setAttribute("align", align);
  180.   else
  181.     globalElement.removeAttribute("align");
  182.  
  183.   if (dialog.shading.checked) {
  184.     shading = true;
  185.     globalElement.removeAttribute("noshade");
  186.   } else {
  187.     shading = false;
  188.     globalElement.setAttribute("noshade", "");
  189.   }
  190.   return true;
  191. }
  192.  
  193. function onOK()
  194. {
  195.   if (ValidateData())
  196.   {
  197.     // Copy attributes from the globalElement to the document element
  198.     editorShell.CloneAttributes(hLineElement, globalElement);
  199.     SaveWindowLocation();
  200.     return true;
  201.   }
  202.   return false;
  203. }
  204.